from flask import render_template,redirect,url_for,session, flash, request, jsonify
from flask_login import login_required, current_user
from controller import app
from service.order import OrderItemService
from bson.objectid import ObjectId
from bson.dbref import DBRef
from model.order import OrderDao

@app.route("/add_to_cart/<string:item_id>")
@app.route("/add_to_cart/<string:item_id>", methods=['GET','POST'])
@login_required
def add_to_cart(item_id):
    # If the user have an existing unchecked order, then generate an order
    order_id = ObjectId(); 
    order_dao = OrderDao();
    user_ref = DBRef('users',current_user._id)
    order_dao.save({'_id':order_id,'title':'customer','created':1,'status':0,'user':user_ref});
    serv = OrderItemService()
    order_ref = DBRef('orders',order_id)
    inputs = {'iid':item_id,'quantity':1,'order':order_ref,'item':1}
    serv.submit_order_item()